package maiyatian.videolive.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import maiyatian.videolive.IActivity;
import maiyatian.videolive.IClickListener;
import maiyatian.videolive.R;
import maiyatian.videolive.adapter.SearchAdapter;
import maiyatian.videolive.bean.LiveBean;
import maiyatian.videolive.pulltorefresh.XListView;

/**
 * Created by 王中阳 on 2016/5/10.
 */
@ContentView(R.layout.activity_search)
public class SearchActivity extends IActivity implements View.OnClickListener, XListView.IXListViewListener, IClickListener {
    public static final String TAG = "SearchActivity";
    @ViewInject(R.id.cancelSearch)
    TextView cancelSearch;
    SearchAdapter adapter;
    List<LiveBean> datas;
    @ViewInject(R.id.xlvSearch)
    XListView xListView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        cancelSearch.setOnClickListener(this);
        initViews();
        xListView.setPullLoadEnable(true);
        xListView.setXListViewListener(this);
    }

    private void initViews() {
        LiveBean liveBean = new LiveBean();
        datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add(liveBean);
        }
        adapter = new SearchAdapter(this, datas, this);
        xListView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancelSearch:
                this.finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        mHandler.sendMessage(mHandler.obtainMessage(102, datas));
    }

    @Override
    public void onLoadMore() {
        mHandler.sendMessage(mHandler.obtainMessage(102, datas));
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 101:
//                    adapter = new ListViewAdapter(activity, cache.generateLocalList());
//                    xlistview.setAdapter(adapter);
//                    //if(adapter != null)
//                    //	adapter.add_updat(cache.generateLocalList());
                    sendEmptyMessage(110);
                    break;
                case 102:
                    //加载数据
//                    cache.update((ArrayList<HolderInfo>) msg.obj);
//                    if (adapter != null) {
//                        adapter.add_updat(cache.generateLocalList());
//                    }
                    sendEmptyMessage(101);
                    break;
                case 103:
                    //加载数据
//                    cache.update_nextPullto((ArrayList<HolderInfo>) msg.obj);
//                    if (adapter != null) {
//                        adapter.add_updat(cache.generateLocalList());
//                    }
                    sendEmptyMessage(110);
                    break;
                case 110:
                    xListView.stopRefresh();
                    xListView.stopLoadMore();
                    xListView.setRefreshTime("刚刚");
                    break;
                default:
                    break;
            }

        }

        ;
    };

    @Override
    public void onItemClicked(int position) {
        showToast(this, position + "");
    }
}
